import { ref, unref } from "vue"

interface hasOneChildren {
  onlyOneChildren: boolean,
  oneChildren: any
}

export const hasOneChildren = (children: any = [], parent: any): hasOneChildren => {

  const oneChildren = ref()
  const childrenFilter = children.filter((item: any) => {
    const meta = item?.meta ?? {}
    if (meta?.hidden) {
      return false
    } else {
      oneChildren.value = item
      return true
    }
  })

  // 只有一个子菜单
  if (childrenFilter.length === 1) {
    return {
      onlyOneChildren: true,
      oneChildren: unref(oneChildren)
    }
  }

  // 没有一个子菜单则返回父级
  if (!children.length) {
    return {
      onlyOneChildren: true,
      oneChildren: { ...parent, path: "", noOneChildren: true }
    }
  }

  return {
    onlyOneChildren: false,
    oneChildren
  }
}


